package com.yulongtian.oneProblemEveryDay.month202211.day20221129;

import java.util.ArrayList;
import java.util.List;

/**
 * 回溯和全排列
 * 可
 *
 * @author yulongTian
 * @create 2022-11-29 16:26
 */
public class Test05 {
    public static void main(String[] args) {
        int[] nums = {1, 2, 3};
        List<List<Integer>> permute = permute(nums);
        System.out.println(permute.toString());
    }

    public static List<List<Integer>> permute(int[] nums) {
        List<List<Integer>> res = new ArrayList<>();

        dfs(nums, res, 0);

        return res;
    }

    public static void dfs(int[] nums, List<List<Integer>> res, int index) {

        if (index >= nums.length) {
            return;
        }

        for (int i = index; i < nums.length; i++) {
            swap(nums, index, i);
            List<Integer> temp = new ArrayList<>(nums.length);
            for (int num : nums) {
                temp.add(num);
            }
//            if (!res.contains(temp)) {
//                res.add(temp);
//            }
            res.add(temp);
            dfs(nums, res, index + 1);
            swap(nums, index, i);
        }


    }

    public static void swap(int[] nums, int i, int j) {
        int temp = nums[i];
        nums[i] = nums[j];
        nums[j] = temp;
    }

}
